package com.example.studentmanagement;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;

import com.example.studentmanagement.dao.LoginDao;
import com.example.studentmanagement.util.DBUtil;
import com.example.studentmanagement.util.ToolsUtil;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);




    //加载数据库
        DBUtil dbUtil = new DBUtil(MainActivity.this);
        SQLiteDatabase db = dbUtil.getWritableDatabase();
        SQLiteDatabase dbd = dbUtil.getWritableDatabase();

    //文本框以及按钮内容获取

        EditText accountT=this.findViewById(R.id.login_username);
        EditText pwdD=this.findViewById(R.id.login_password);


        Button login=this.findViewById(R.id.login_button_login);

        RadioButton user=findViewById(R.id.login_user);
        user.setChecked(true);
        RadioButton man=findViewById(R.id.login_manage);

        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String account=ToolsUtil.editTextToString(accountT);
                String pwd=ToolsUtil.editTextToString(pwdD);

                SharedPreferences sharedPreferences = getSharedPreferences("loginId" , Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putString("s_id" , account);
                editor.apply();
                int admin=0;


                if(account.isEmpty()){
                    Toast.makeText(MainActivity.this,"请输入学号",Toast.LENGTH_SHORT).show();
                }else if (pwd.isEmpty()){
                    Toast.makeText(MainActivity.this,"请输入密码",Toast.LENGTH_SHORT).show();
                }else{

                    if (user.isChecked()){
                        int res= LoginDao.LoginUse(db,account,pwd);
                        if (res==0){
                            Toast.makeText(MainActivity.this,"账号密码错误，请重试！",Toast.LENGTH_SHORT).show();
                        }else{

                            Toast.makeText(MainActivity.this,"用户登陆成功",Toast.LENGTH_SHORT).show();
                            Intent intent =new Intent(MainActivity.this, StudentListActivity.class);
                            startActivity(intent);
                            finish();
                        }



                    }else if (man.isChecked()){
                        int res= LoginDao.LoginMan(dbd,account,pwd);
                        if (res==0){
                            Toast.makeText(MainActivity.this,"账号密码错误，请重试！",Toast.LENGTH_SHORT).show();
                        }else{
                            admin = admin + 1;
                            Log.d("测试admin是否存在", "`````````"+admin);

                            Toast.makeText(MainActivity.this,"管理员登陆成功",Toast.LENGTH_SHORT).show();
                            Intent intent =new Intent(MainActivity.this, StudentListActivity.class);
                            startActivity(intent);
                            finish();
                        }
                    }else{
                        Toast.makeText(MainActivity.this,"请选择登录权限",Toast.LENGTH_SHORT).show();
                    }
                }

                SharedPreferences sharedPreferences1 = getSharedPreferences("admin" , Context.MODE_PRIVATE);
                SharedPreferences.Editor editor1 = sharedPreferences1.edit();
                editor1.putInt("admin" , admin);
                editor1.apply();



            }
        });




    }
}